﻿using log4net;
using ServerControl.Dal;
using ServerControl.Model;
using ServerControl.WebManage.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServerControl.Utils;
using ServerControl.Model.Enum;
using System.Text;
using MiniExcelLibs;
using System.IO;
using System.Web.Http.Results;
using static System.Net.Mime.MediaTypeNames;
using WebGrease.Css.Extensions;
using MiniExcelLibs.OpenXml;
using System.Security.Principal;
using Newtonsoft.Json;

namespace ServerControl.WebManage.Controllers.Admin
{
    [RoutePrefix("admin/comment")]
    [Auth(AllowRole = Model.Enum.Role.AdminOrUser)]
    public class CommentController : BaseController
    {
        private ILog logger = LogManager.GetLogger("CommentController");

        [Route("index")]
        public ActionResult Index(int videoId = 0)
        {
            ViewBag.VideoId = videoId;
            return View();
        }


        [HttpPost]
        [Route("list")]
        public JsonResult ListPage(int pageIndex = 1,
            int pageSize = 20,
            int videoId = 0,
            string uniqueId = "",
            string nickName = "",
            ContentTypeEnum contentType = ContentTypeEnum.All,
            CommentStatusEnum commentStatus = CommentStatusEnum.All,
            string clientName = "")
        {
            try
            {
                int records = 0;
                var datas = CommentDal.ListPage(ref records, pageIndex, limit: pageSize, CurrentUser.UserName, videoId, uniqueId, nickName, contentType, commentStatus, clientName);
                return new JsonNetResult { Data = new SuccessResult<List<Comment>> { Data = datas, Count = records, Msg = "" } };
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
        }

        [HttpPost]
        [Route("delete")]
        public JsonResult Delete(List<int> ids)
        {
            try
            {
                var comments = CommentDal.Select(CurrentUser.UserName, ids).Where(c => c.ContentType == ContentTypeEnum.Image).ToList();

                if (CommentDal.Delete(CurrentUser.UserName, ids))
                {
                    comments.ForEach(c =>
                    {
                        try
                        {
                            var filePath = Server.MapPath(c.Content);

                            if (System.IO.File.Exists(filePath))
                            {
                                System.IO.File.Delete(filePath);
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                        }
                    });

                    return new JsonNetResult { Data = new SuccessResult { Msg = "删除成功" } };
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "删除失败" } };
        }

        [HttpPost]
        [Route("edit")]
        public JsonResult Edit(Comment comment)
        {
            try
            {
                comment.UserName = CurrentUser.UserName;

                if (CommentDal.Update(comment))
                {
                    return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
        }

        [HttpPost]
        [Route("add")]
        public JsonResult Add(Comment comment)
        {
            try
            {
                comment.UserName = CurrentUser.UserName;
                comment.AddTime = DateTime.Now;
                comment.CommentTime = comment.AddTime;
                comment.ClientName = string.Empty;
                comment.NickName = string.Empty;
                comment.UniqueId = string.Empty;

                if (CommentDal.Insert(comment))
                {
                    return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
        }
    }
}